﻿Imports System.Data.SqlClient
Public Class frmSoCai

    Private Sub frmSoCai_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CapNhatLuoi()
    End Sub

    Private Sub CapNhatLuoi()
        Dim strSql As String = "select ROW_NUMBER() OVER (ORDER BY sc.NgayCT) as STT, sc.LoaiCT as [Loại CT], sc.SoCT as [Số CT], sc.SoHD as [Số HĐ],"
        strSql &= " kh.TenKH as [Tên KH], convert(VARCHAR,cast(sc.TongTien as money),1) as [Tổng Tiền], convert(VARCHAR, NgayCT,103) as [Ngày CT], sc.MaPhieu from SoCai sc left join KhachHang kh on sc.MaKH = kh.MaKH where 1=1 "
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myComm As New SqlCommand()
            myComm.Connection = myConn
            If chkNgay.Checked Then
                strSql &= " and (sc.NgayCT >= convert(DATETIME, '" & dtpFrom.Value.ToString("dd/MM/yyyy") & "', 103) and sc.NgayCT <= convert(DATETIME, '" & dtpTo.Value.ToString("dd/MM/yyyy") & "', 103))"
            End If
            If chkLoai.Checked Then
                strSql &= " and ( 1=0 "
                If chkNhapKho.Checked Then
                    strSql &= " or LoaiCT = 'PNK' "
                End If
                If chkXuatKho.Checked Then
                    strSql &= " or LoaiCT = 'PXK' "
                End If
                strSql &= ")"
            End If
            If chkKhachHang.Checked Then
                strSql &= " and kh.TenKH like @TenKH "
                myComm.Parameters.AddWithValue("@TenKH", "%" & txtKhachHang.Text & "%")
            End If
            If chkGiaTri.Checked Then
                strSql &= " and sc.TongTien >= " & txtThap.Text & " and sc.TongTien <= " & txtCao.Text
            End If
            strSql &= " order by STT"
            myComm.CommandText = strSql
            Dim myAdapter As New SqlDataAdapter(myComm)
            myAdapter.Fill(myTable)
            dgvList.DataSource = myTable
            For Each row As DataRow In myTable.Rows
                row(5) = CDbl(row(5)).ToString("#,###")
            Next
            dgvList.Columns.Item(0).Width = 50
            dgvList.Columns.Item(1).Width = 100
            dgvList.Columns.Item(2).Width = 100
            dgvList.Columns.Item(3).Width = 100
            dgvList.Columns.Item(4).Width = 300
            dgvList.Columns.Item(5).Width = 150
            dgvList.Columns.Item(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            dgvList.Columns.Item(6).Width = 150
            dgvList.Columns.Item(7).Visible = False
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
    End Sub

    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        If chkGiaTri.Checked And (Not IsNumeric(txtThap.Text) Or Not IsNumeric(txtCao.Text)) Then
            MsgBox("Giá trị không hợp lệ")
        Else
            CapNhatLuoi()
        End If
    End Sub

    Private Sub btnNhapKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapKho.Click
        Dim frm As New frmNhapKho(-1)
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.ShowDialog()
    End Sub

    Private Sub btnXuatKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXuatKho.Click
        Dim frm As New frmXuatKho(-1)
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.ShowDialog()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If Me.dgvList.SelectedRows.Count > 0 Then
            If Me.dgvList.SelectedRows.Item(0).Cells(1).Value = "PNK" Then
                Dim frm As New frmNhapKho(Me.dgvList.SelectedRows.Item(0).Cells(7).Value)
                frm.ShowDialog()
            Else
                Dim frm As New frmXuatKho(Me.dgvList.SelectedRows.Item(0).Cells(7).Value)
                frm.ShowDialog()
            End If
        Else
            MsgBox("Vui lòng chọn dòng cần sửa!")
        End If
    End Sub

    Private Sub btnDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub dgvList_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvList.CellDoubleClick
        If Me.dgvList.SelectedRows.Count > 0 Then
            If Me.dgvList.SelectedRows.Item(0).Cells(1).Value = "PNK" Then
                Dim frm As New frmNhapKho(Me.dgvList.SelectedRows.Item(0).Cells(7).Value)
                frm.ShowDialog()
            Else
                Dim frm As New frmXuatKho(Me.dgvList.SelectedRows.Item(0).Cells(7).Value)
                frm.ShowDialog()
            End If
        End If
    End Sub

    Private Sub chkNgay_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkNgay.CheckedChanged
        If chkNgay.Checked Then
            dtpFrom.Enabled = True
            dtpTo.Enabled = True
        Else
            dtpFrom.Enabled = False
            dtpTo.Enabled = False
        End If
    End Sub

    Private Sub chkLoai_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkLoai.CheckedChanged
        If chkLoai.Checked Then
            chkNhapKho.Enabled = True
            chkXuatKho.Enabled = True
        Else
            chkNhapKho.Enabled = False
            chkXuatKho.Enabled = False
        End If
    End Sub

    Private Sub chkKhachHang_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkKhachHang.CheckedChanged
        If chkKhachHang.Checked Then
            txtKhachHang.ReadOnly = False
        Else
            txtKhachHang.ReadOnly = True
        End If
    End Sub

    Private Sub chkGiaTri_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGiaTri.CheckedChanged
        If chkGiaTri.Checked Then
            txtThap.ReadOnly = False
            txtCao.ReadOnly = False
        Else
            txtThap.ReadOnly = True
            txtCao.ReadOnly = True
        End If
    End Sub
End Class